package edu.zjut.hjg.saas.kwmp.web.controller.teacher;

import static edu.zjut.hjg.saas.kwmp.web.support.KwmpConstants.SESSION_NAME_USER;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.zjut.hjg.saas.biz.model.Kindergarten;
import edu.zjut.hjg.saas.biz.model.SchoolClass;
import edu.zjut.hjg.saas.biz.service.SchoolClassService;
import edu.zjut.hjg.saas.common.annotation.CookieSessionIn;
import edu.zjut.hjg.saas.common.annotation.RequestAttribute;
import edu.zjut.hjg.saas.common.util.ParameterUtil;
import edu.zjut.hjg.saas.kwmp.web.model.UserSession;

@Controller
@RequestMapping("/teacher/schoolClass")
public class SchoolClassManageController {
    private static final Log   log = LogFactory.getLog(SchoolClassManageController.class);

    @Autowired
    private SchoolClassService schoolClassService;

    @RequestMapping("index.htm")
    public String index(@RequestAttribute Kindergarten app, Model model) {
        model.addAttribute("schoolClasses", schoolClassService.listByAppId(app.getId()));
        return "teacher/schoolClass";
    }

    @CookieSessionIn(value = SESSION_NAME_USER, param = "session")
    @RequestMapping(value = "create.htm", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> create(@RequestAttribute Kindergarten app, UserSession session,
                                      String name) {
        Map<String, Object> result = new HashMap<String, Object>();
        if (!ParameterUtil.notBlank(name)) {
            result.put("success", false);
            result.put("message", "班级名称不能为空");
            return result;
        }
        SchoolClass schoolClass = new SchoolClass();
        schoolClass.setKindergarten(app);
        schoolClass.setName(name);
        try {
            schoolClassService.create(schoolClass, session.getLoginId());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.put("success", false);
            result.put("message", "创建班级失败");
            return result;
        }
        result.put("success", true);
        result.put("schoolClass", schoolClass);
        return result;
    }

    @CookieSessionIn(value = SESSION_NAME_USER, param = "session")
    @RequestMapping("delete.htm")
    @ResponseBody
    public Map<String, Object> delete(@RequestAttribute Kindergarten app, UserSession session,
                                      @RequestParam("id") int id) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            schoolClassService.deleteByIdAndAppId(id, app.getId(), session.getLoginId());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.put("success", false);
            result.put("message", "删除班级失败");
            return result;
        }
        result.put("success", true);
        return result;
    }

    @CookieSessionIn(value = SESSION_NAME_USER, param = "session")
    @RequestMapping("update.htm")
    @ResponseBody
    public Map<String, Object> update(@RequestAttribute Kindergarten app, UserSession session,
                                      SchoolClass schoolClass) {
        Map<String, Object> result = new HashMap<String, Object>();
        if (schoolClass == null
                || !ParameterUtil.notBlank(schoolClass.getId(), schoolClass.getName())) {
            result.put("success", false);
            result.put("message", "参数错误");
            return result;
        }
        schoolClass.setKindergarten(app);
        try {
            schoolClassService.update(schoolClass, app.getId(), session.getLoginId());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.put("success", false);
            result.put("message", "更新班级失败");
            return result;
        }
        result.put("success", true);
        return result;
    }
}
